<div class="modal-header">
	<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><li class="fa fa-remove"></li></button>
	<h5 class="modal-title">修改用户角色</h5>
</div>

<div class="modal-body">

	<form id="user-form" name="user-form" class="form-horizontal">
		<!--<input type="hidden" name="id">
		<input type="hidden" name="version">
		<input type="hidden" name="isSuperAdmin">
		<input type="hidden" name="createDateTime" data-flag="date" data-format="yyyy-MM-dd HH:mm:ss">
		<input type="hidden" name="deleted">
		<input type='hidden' value='${CSRFToken}' id='csrftoken'>-->
		<div class="box-body">
			<div class="col-md-10">
				<div class="form-group">
					<label for="loginName" class="col-sm-4 control-label">用户名</label>

					<div class="col-sm-8">
						<input type="text" class="form-control" id="userName" name="userName">
					</div>
				</div>
				<!--<div class="form-group">
					<label class="col-sm-3 control-label">角色</label>

					<div class="col-sm-8">
						<select name="role" class="form-control" id="role">
							<option value="1">1</option>
							<option value="2">2</option>
							<option value="3">3</option>
						</select>
					</div>

				</div>-->
				<div class="form-group">
					<label class="col-sm-4 control-label">角 &nbsp;&nbsp;色</label>

					<!--列表树容器-->
					<div id="tree" class="col-sm-8"></div>

				</div>
			</div>
		</div>
		<!-- /.box-body -->
		<div class="box-footer text-right">
			<!--以下两种方式提交验证,根据所需选择-->
			<button type="button" class="btn btn-default" data-btn-type="cancel" data-dismiss="modal">取消</button>
			<button type="button" class="btn btn-primary" data-btn-type="save">提交</button>
		</div>

	</form>

</div>
<script>
	function getTree() {
		//Some logic to retrieve, or generate tree structure（一些逻辑检索，或生成树结构）
		var tree = [{
			text: "角色等级",

			nodes: [{
					text: "1 超级管理员"
				},
				{
					text: "2 普通管理员"
				},
				{
					text: "3 普通用户"
				}
			]
		}];
		return tree;
	}
	$("#tree").treeview({
		data: getTree()
	});

	$(function() {
		var userrole = sessionStorage.getItem('userrole');
		roleEntity = JSON.parse(userrole);
		$("#userName").val(roleEntity.userrole);
		var id = roleEntity.roleid;
		$id = $($("#tree").get(0).children[0].children[id]);
		$id.addClass('node-selected');
		$(".btn-primary").click(function(event) {
			var rol = document.getElementsByClassName("node-selected");
			var roleid = rol[0].getAttribute('data-nodeid');
			console.log(roleid);
			if(roleid != 0) {
				
				$.ajax({
					type: "post",
					url: "/role/update",
					data: {
						username: $("#userName").val(),
						roleid: roleid
					},
					success: function(data) {
						data = JSON.parse(data);
						//					console.log(data.code);
						if(data.code == 0) {
							alert("修改成功");
							location.href = "/role/list";

							//						session.removeAttribute("userinformation");
							sessionStorage.removeItem("userrole");
						} else {
							alert(data.message);
							event.preventDefault();
						}
					},
					error: function(er) {
						console.log(er);
					}
				});
			} else {
				alert("请选择角色！");
			}

		});
	});
</script>